package cn.jy.careuplusdoctor.mycenter;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import cn.jy.careuplusdoctor.R;
import cn.jy.careuplusdoctor.base.BaseActivity;
import cn.jy.careuplusdoctor.entity.DoctorRecs;
import cn.jy.careuplusdoctor.http.HttpHelper;
import cn.jy.careuplusdoctor.http.HttpHelper.onHttpListener;
import cn.jy.careuplusdoctor.http.HttpUrls;
import cn.jy.careuplusdoctor.injector.IdInject;
import cn.jy.careuplusdoctor.response.DoctorRecsResponse;
import cn.jy.careuplusdoctor.response.LoginResponse;
import cn.jy.careuplusdoctor.utils.StringUtil;
import cn.jy.careuplusdoctor.widgets.CareuTitleBar.TitleBarLeftAction;
import cn.jy.careuplusdoctor.widgets.CareuTitleBar.TitleBarRightAction;
import fay.frame.fast.F;

public class MineInfoActivity extends BaseActivity implements TitleBarLeftAction,TitleBarRightAction{
	//医生代码
	@IdInject(id = R.id.txt_doctor_code)
	private TextView txt_doctor_code;
	//真实姓名
	@IdInject(id = R.id.txt_fullname)
	private TextView txt_fullname;
	//手机号
	@IdInject(id = R.id.txt_phone)
	private TextView txt_phone;
	//性别
	@IdInject(id = R.id.txt_sex)
	private TextView txt_sex;
	//毕业证书
	@IdInject(id = R.id.txt_certificate)
	private TextView txt_certificate;	
	//所在医院
	@IdInject(id = R.id.edit_hospital)
	private EditText edit_hospital;	
	//所在科室
	@IdInject(id = R.id.edit_section_office)
	private EditText edit_section_office;	
	//职称
	@IdInject(id = R.id.edit_job_title)
	private EditText edit_job_title;	
	//邮箱
	@IdInject(id = R.id.edit_mail)
	private EditText edit_mail;	
	//毕业证
	@IdInject(id = R.id.img_certificate)
	private ImageView img_certificate;
	
	@IdInject(id = R.id.image_hospital)
	private ImageView	image_hospital;
	@IdInject(id = R.id.image_section_office)
	private ImageView	image_section_office;
	@IdInject(id = R.id.image_job_title)
	private ImageView	image_job_title;
	@IdInject(id = R.id.image_mail)
	private ImageView	image_mail;
	
	private EditText[]  editTexts = null;
	private ImageView[] imageArrowss = null;
	//判断编辑
	private boolean isEdit = false;
	private String doctorId;
	private String hid,sid;
	private DoctorRecs doctord;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mine_info);
		initTitleBar();
		onInJectView();
		init();
		
		edit_hospital.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Intent i = new Intent(MineInfoActivity.this, HospitalListActivity.class);
					i.putExtra("ListString", "医院");
					i.putExtra("activity", "info");
					startActivityForResult(i, 1001);
				}
				return true;
			}
		});
		edit_section_office.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if(hid!=null){
						Intent i = new Intent(MineInfoActivity.this, HospitalListActivity.class);
						i.putExtra("ListString", "科室");
						i.putExtra("activity", "info");
						i.putExtra("hid", hid);
						startActivityForResult(i, 1001);
					}else{
						shortToast("请先选择医院");
					}
				}
				return true;
			}
		});
	}
	@Override
	protected void settingTitleBar() {
		super.settingTitleBar();
		getTitleBar().setLeftText("更多");
		getTitleBar().setTitle("个人信息");
		getTitleBar().setLeftAction(MineInfoActivity.this);
		getTitleBar().getLeftImageView().setImageResource(R.drawable.title_bar_back);
		getTitleBar().setRightText("编辑");
		getTitleBar().setRightAction(this);
		getTitleBar().getRightImageView().setVisibility(View.GONE);
	}
	public void init() {
		imageArrowss = new ImageView[] {image_hospital,image_section_office,image_job_title,image_mail};
		editTexts= new EditText[] {edit_hospital,edit_section_office,edit_job_title,edit_mail};
		SharedPreferences shared = getSharedPreferences("careu_doctor",  //缓存
				Activity.MODE_PRIVATE);
		doctorId = shared.getString("doctorId", "");
		hid=shared.getString("hid", "");
		showLoading("正在获取");
		HttpHelper.getInstance().coctorInfo(doctorId, new onHttpListener<DoctorRecsResponse>() {
			@Override
			public void onResponse(DoctorRecsResponse data, int code) {
				hidenLoading();
				if (data != null && code == HttpUrls.REQUEST_SUCCESS) {
					DoctorRecs doctor = data.getDoctorRecs();
					if (TextUtils.equals("1", data.getResType()) && doctor != null) {
						sid = doctor.getSid();
						doctord = doctor;
						coctorInfo(doctord);
					} else {
						shortToast(data.getMsg());
					}
				} else {
					shortToast("网络故障，获取信息失败");
				}
			}
		});
	}
	/**
	 * 显示个人信息
	 */
	public void coctorInfo(DoctorRecs doctor) {
		txt_doctor_code.setText(doctor.getqDoctorCode());
		txt_fullname.setText(doctor.getdName());
		txt_phone.setText(doctor.getPhone());
		if (StringUtil.isNotEmpty(doctor.getSex())) {
			if (doctor.getSex().equals("0")) {
				txt_sex.setText("男");
			} else {
				txt_sex.setText("女");
			}
		} else {
			txt_sex.setText("");
		}
		edit_hospital.setText(doctor.getqHospital());
		edit_section_office.setText(doctor.getqSubject());
		edit_job_title.setText(doctor.getMedicalTitle());
		edit_mail.setText(doctor.getEmail());
		txt_certificate.setText(doctor.getLicenceCode());
		F f = new F(MineInfoActivity.this);
		f.id(img_certificate).image(HttpUrls.BASE_HOST+doctor.getLicencePicUrl());
	}
	/**
	 * 编辑个人信息
	 */
	public void editInfo() {
			String hospital = edit_hospital.getText().toString();
			String section_office = edit_section_office.getText().toString();
			String job_title = edit_job_title.getText().toString();
			String mail = edit_mail.getText().toString();
			if (!StringUtil.isEmail(mail)) {
				shortToast("正确填写邮箱格式");
				return;
			}
			else if(mail.trim().length()>50){
				shortToast("邮箱不能超过50个字符");
				return;
			}else if(job_title.trim().length()>50){
				shortToast("职称不能超过50个字符");
				return;
			}
			else if (!StringUtil.isNotEmpty(job_title)) {
				shortToast("职称不能为空");
				return;
			}
			showLoading("正在修改");
			HttpHelper.getInstance().doctorUpdate(doctorId, hospital,section_office,job_title,mail, new onHttpListener<LoginResponse>() {
				@Override
				public void onResponse(LoginResponse data, int code) {
					hidenLoading();
					if (data !=null && code == HttpUrls.REQUEST_SUCCESS) {
						if (TextUtils.equals("1", data.getResType())) {
							complete();
							shortToast("修改成功");
						}
						else {
							edit();
							shortToast("修改失败");
						}
					}
					else {
						shortToast("网络故障，修改失败");
					}
				}
			});
	}
	@Override
	public void onLeftPress(View view) {
		if (isEdit) {
			complete();
			if (doctord != null) {
				coctorInfo(doctord);
			}
		} else {
			defaultFinish();
		}
	}
	@Override
	public void onRightPress(View view) {
		// 编辑
		if (isEdit) {
			editInfo();
		} else {
			edit();
		}
	}
	/**
	 * 点击完成的状态
	 */
	public void complete() {
		isEdit = false;
		getTitleBar().setLeftText("更多");
		getTitleBar().getLeftImageView().setVisibility(View.VISIBLE);
		getTitleBar().setRightText("编辑");
		for (int i = 0; i < imageArrowss.length; i++) {
			imageArrowss[i].setVisibility(View.GONE);
		}
		for (int i = 0; i < editTexts.length; i++) {
			editTexts[i].setEnabled(false);
		}
		//上传
	}
	/**
	 * 点击编辑的状态
	 */
	public void edit() {
		isEdit = true;
		getTitleBar().setLeftText("取消");
		getTitleBar().getLeftImageView().setVisibility(View.GONE);
		getTitleBar().setRightText("完成");
		for (int i = 0; i < imageArrowss.length; i++) {
			imageArrowss[i].setVisibility(View.VISIBLE);
		}
		for (int i = 0; i < editTexts.length; i++) {
			editTexts[i].setEnabled(true);
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		 if(resultCode==1002){
			Bundle b=data.getExtras(); 
			edit_hospital.setText(b.getString("name"));
			hid=b.getString("id");
		}else if(resultCode==1003){
			Bundle b=data.getExtras(); 
			edit_section_office.setText(b.getString("name"));
			sid=b.getString("id");
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
